#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct student{
	string id;
	string name;
	int score;
};

student stu[100000];


 bool cmp(student a,student b){
	return a.id<b.id;
}
bool cmp2(student a,student b){
	if(a.name==b.name){
		return a.id<b.id;
	}
	return a.name<b.name;
}

bool cmp3(student a,student b){
	if(a.score==b.score){
		return a.id<b.id;
	}
	return a.score<b.score;
}

void way1();
void way2();
void way3();

int N,C;
int num=1;
int main()
{
	
	while(cin>>N>>C){
		if(N==0) break;
		for(int i=0;i<N;i++)
		{
			cin>>stu[i].id>>stu[i].name>>stu[i].score;
		}
		switch(C){
			case 1:
				way1();
				num++;
				break;
			case 2:
				way2();
				num++;
				break;
			case 3:
				way3();
				num++;
				break;	
		}
	}
}
void way1()
{
	sort(stu,stu+N,cmp);
	printf("Case %d:\n",num);
	for(int i=0;i<N;i++){
		printf("%s %s %d\n",stu[i].id.c_str(),stu[i].name.c_str(),stu[i].score);
		//cout<<stu[i].id<<" "<<stu[i].name<<stu[i].score<<endl;
	}
}

void way2()
{
	
	sort(stu,stu+N,cmp2);
	printf("Case %d:\n",num);
	for(int i=0;i<N;i++){
			printf("%s %s %d\n",stu[i].id.c_str(),stu[i].name.c_str(),stu[i].score);
	}
}

void way3()
{

	sort(stu,stu+N,cmp3);
    printf("Case %d:\n",num);
	for(int i=0;i<N;i++){
			printf("%s %s %d\n",stu[i].id.c_str(),stu[i].name.c_str(),stu[i].score);
	}
}

























